package com.example.capsule.repository;

import com.example.capsule.entity.Invitation;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

@Repository
public interface InvitationRepository extends JpaRepository<Invitation, Long> {
    Optional<Invitation> findByInviteToken(String inviteToken);

    List<Invitation> findByRecipientPhone(String recipientPhone);

    List<Invitation> findByCapsuleId(Long capsuleId);

    Optional<Invitation> findByCapsuleIdAndRecipientPhone(Long capsuleId, String recipientPhone);

    @Query("SELECT i FROM Invitation i WHERE i.status = :status AND i.expiresAt < :currentTime")
    List<Invitation> findByStatusAndExpiresAtBefore(@Param("status") String status,
                                                    @Param("currentTime") LocalDateTime currentTime);

    long countByRecipientPhoneAndStatus(String recipientPhone, String status);

    boolean existsByRecipientPhoneAndStatus(String recipientPhone, String status);
}